package com.davidhigenbottam.HeatMap.server;

import java.io.IOException;
import java.util.Random;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.davidhigenbottam.HeatMap.data.ProvinceData;
import com.davidhigenbottam.HeatMap.shared.Query;
import com.davidhigenbottam.HeatMap.shared.ServerResults;

@SuppressWarnings("serial")
public class KMLServlet extends HttpServlet {
	
	
	
//	private static final Logger log = Logger.getLogger(KMLServlet.class
//			.getName());
	
	
	public static boolean localMode = false;

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("application/vnd.google-earth.kml+xml");

		Query q = new Query(req.getParameter("ds"), req.getParameter("y"),
				req.getParameter("l"), req.getParameter("it"),
				req.getParameter("io"));
		
		localMode = (req.getServerName().equals("localhost") || req
				.getServerName().equals("127.0.0.1"));
		
		
		generateHeatmapKML(q,resp);	
		
		
		//generateInstitutionSymbolKML(q,resp);	
		


		
	}

	private void generateHeatmapKML(Query q, HttpServletResponse resp) throws IOException{
		DataCache cache= DataCache.getInstance();
		ServerResults res = cache.getData(q);
		
		ProvinceData provinceData= res.getProvinceData();
		
		
		ProvinceKML kml = ProvinceKML.getInstance(getServletContext()
				.getRealPath("/Indo_provinces.kml"));
		kml.resetColours();
		kml.setColours(provinceData);
		kml.setLegendBands(provinceData);
		kml.stream(resp.getWriter());		
	}
	
	
//	private void generateInstitutionSymbolKML(Query q, HttpServletResponse resp) throws IOException{
//		DataCache cache= DataCache.getInstance();
//		ServerResults res = cache.getData(q);
//		
//		InstitutionData institutionData=  res.getInstitutionData();
//		
//		//todo create the KML
//		PrintWriter out= resp.getWriter();
//		out.println("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>");
//		out.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
//		out.println("<Document>");
//		out.println("<Folder>");
//		
//		for(Institution i : institutionData.getInstitutions())
//		{
//			out.println("<Placemark>");
//			out.print("<name>");
//			out.print(i.getName());
//			out.println("</name>");
//			out.println("<Style><IconStyle>");
//			out.print("<scale>");
//			out.print(institutionData.getIconScale(i));
//			out.println("</scale>");
//			out.println("<Icon>");
//			out.print("<href>");
//			if(i.isPrivate())
//				out.print(UI.PRIVATE_INSTITUTE_ICON);
//			else
//				out.print(UI.PUBLIC_INSTITUTE_ICON);
//			
//			out.println("</href>");
//			out.println("</Icon></IconStyle></Style>");
//			out.print("<description><![CDATA[");
//			out.print(i.getName());
//			out.print("<br/>");
//			out.print(i.getUrl());
//			out.print("<br/>");
//			out.print("Total:");
//			out.print(i.getValueDescription());
//			out.println("]]></description>");
//			out.println("<Point><coordinates>");
//			out.print(i.getGeocode());
//			out.println("</coordinates></Point>");
//			out.println("</Placemark>");	
//		}
//		
//		out.println("");
//		
//		
//		out.println("</Folder></Document></kml>");
//		

//	}	
	
	/**
	 * generate a random hex color
	 * 
	 * @return
	 */
	public static String getRandomColor() {

		String hex1 = getRandomHex();
		String hex2 = getRandomHex();
		String hex3 = getRandomHex();
		String hex4 = getRandomHex();
		String hex5 = getRandomHex();
		String hex6 = getRandomHex();

		String color = "7f" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;

		return color;
	}

	/**
	 * get random hex
	 * 
	 * @return int
	 */
	private static String getRandomHex() {
		String[] hex = new String[] { "0", "1", "2", "3", "4", "5", "6", "7",
				"8", "9", "A", "B", "C", "D", "E", "F" };
		Random random = new Random();
		int randomNum = random.nextInt(hex.length);
		String sHex = hex[randomNum];
		return sHex;
	}



}
